BB_URL = 'http://zagorski.im.pwr.wroc.pl/scantegrity/codes/ward3_codes.xml';

function showBallot(ballot){
	var $ballot = $(ballot);
	var $questions = $ballot.find('question');

	var w = '';
	$questions.each(function(i){
		w += showQuestion(this);
	});

	return '<div class="ballot"><div class="webserial">' + $ballot.attr('webSerial') + '</div>' + w + '</div>';
}

function showQuestion(question) {
	var $q = $(question);
	var $symbols = $q.find('symbol');
	
	var w = '';
	$symbols.each(function(i){
		w += showSymbol(this);
	});

	return '<div class="question">' + $q.attr("id") + ") question " + w +	"</div>";
}

function showSymbol(symbol) {
	var $s = $(symbol);
	
	return '<div class="symbol">' + $s.attr('id') + "] " + $s.attr("code") + '</div>';
}


function verify(data){
	//WebLogin to track (that user entered)
	var webserial = $('#webserial').val();
	if(webserial == ''){ 
		webserial = false; 
	}

	//all values from Bulletin Board
	var $ballots = $(data).find('ballot');

	//this will be different for all elections
	var currentHash = 'FirstEntry';

	//output, where we are 'writing'
	var $output = $('#output');
	var $ballot_found = false; //$('<div id="balllot_weblogin"></div>');
	//var $outputBB = $('#outputBB');
      
	//find good ballot
	$ballots.each(function(i){
		var $elem = $(this);
		if(webserial == $elem.attr('webSerial')){
			$ballot_found = $elem.clone();
		}
	});
  
	//$output.append('<div id="ballot_weblogin">' + showBallot($ballot_found) + '</div>');
	if($ballot_found){
		$output.append('<div class="bigskip" style="font-weight:bold;margin-bottom:5px;">Ballot with entered webSerial:</div>' + showBallot($ballot_found));
	}
	else{
		$output.append('<div class="err">Bulletin Board does not contain ballot with entered webSerial!</div>');
	}
	
	hideLoader();
	$('#step3').show();
}

//load specific css code (the same as remotegrity)
$('head').append('<link rel=stylesheet HREF="css/remotegrity.css" type="text/css">');

$('#btn_step2').click(function(){ showLoader('#step2'); $.get(BB_URL, verify); });

hideLoader();